<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/5
     * Time: 下午6:32
     */

    namespace app\store\model;


    use  think\Model;
    use app\common\utils\UtilEncryption;

    class OrderInfoModel extends Model
    {

        protected $name = 'store_order_info';

        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i','update_time' => 'timestamp:Y-m-d H:i'];

        //获取分红金额
        static public function dividend($order_sn)
        {

            $wallet_m  = 0;
            $orderInfo = self::where(['order_sn' => $order_sn])->field('goods_id,goods_num')->select();
            foreach ($orderInfo as $key => $value)
            {
                $royalty  = Store::get($value['goods_id'])->royalty;
                $wallet_m += $royalty * $value['goods_num'];
            }
            return $wallet_m;
        }

        static public function careateRecord($order_sn,$member_id)
        {

            $wallet['money']     = self::dividend($order_sn);
            $wallet['record_sn'] = UtilEncryption::makeOrderSn($member_id);
            $wallet['from_uid']  = 0;
            $wallet['to_uid']    = $member_id;
            PaygatewayWalletRecordModel::create($wallet);
        }

        //触发购买总量更新
        static public function memberBuyUpdate($order_sn)
        {

            $buy_num   = 0;
            $orderInfo = self::where(['order_sn' => $order_sn])->field('goods_id,goods_num')->select();
            foreach ($orderInfo as $key => $value)
            {
                $buy_num += $value['goods_num'];
            }
            //触发购买量更新
            StoreUserModel::shareUpdate($orderInfo['seller_id'],'',StoreUserModel::BUY,$buy_num);
        }

    }